using System;
using System.Collections;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowProcesses
{
	//*********************************************************************
	//
	// XmlActivities Class
	//
	// The XmlActivities class represents a collection base of XmlActivity
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XmlActivities Has no fields
	//
	//*********************************************************************
	public class XMLActivities: bOS.XML.Data.XMLObjectSet
	{
		public const string MAIN_NODE	= "Activities";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLActivities()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLActivities(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		public override void Init()
		{
			this.RemoveAll();
		}

		public override void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLActivity.MAIN_NODE:
						this.Add ( new XMLActivity(node));
						break;
				}
			}
		}


		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLActivities.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** Method to return an activity from collection **/
		public XMLActivity Get (string sActivityName)
		{
			XMLActivity activity;

			for ( int i=0; i<this.List.Count;i++)
			{
				activity = (XMLActivity)this[i];

				if ( activity.Name == sActivityName )
				{
					return activity;
				}
			}

			throw new Scenario.WorkflowEngine.DataModel.Exception.ActivityNotFound(sActivityName);
		}

		/** WAPI Wrappers to call SCENARIO ENGINE **/

		/** TODO 
		
		**/

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Type,
			Description,
			ReferenceActivity,
			CatchType,
			ProcessId
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Type:
					InnerList.Sort(new TypeComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				case SortFields.ReferenceActivity:
					InnerList.Sort(new ReferenceActivityComparer());
					break;
				case SortFields.CatchType:
					InnerList.Sort(new CatchTypeComparer());
					break;
				case SortFields.ProcessId:
					InnerList.Sort(new ProcessIdComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class TypeComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.Type.CompareTo(second.Type);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.Description.CompareTo(second.Description);
			}
		}

		private sealed class ReferenceActivityComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.ReferenceActivity.CompareTo(second.ReferenceActivity);
			}
		}

		private sealed class CatchTypeComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.CatchType.CompareTo(second.CatchType);
			}
		}

		private sealed class ProcessIdComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLActivity first = (XMLActivity) x;
				XMLActivity second = (XMLActivity) y;
				return first.ProcessId.CompareTo(second.ProcessId);
			}
		}
	}
}